/* app */
const Koa = require('koa')
const fs = require('fs')
const app = new Koa()

const Router = require('koa-router')

/* 子路由1： home */
const home = new Router();

home.get('/', async ctx => {
  ctx.body = `
  <ul>
    <li><a href="/page/helloworld">/page/helloworld</a></li>
    <li><a href="/page/404">/page/404</a></li>
  </ul>
  `
})

/* 子路由2：page */
const page = new Router()
page
  .get('/404', async ctx => {
    ctx.body = '404 page'
  })
  .get('/helloworld', async ctx => {
    ctx.body = 'helloworld page!'
  })


const router = new Router()
/* 装载所有子路由 */
router.use('/', home.routes(), home.allowedMethods())
router.use('/page', page.routes(), page.allowedMethods())

/* 加载路由 */
app.use(router.routes()).use(router.allowedMethods())

app.listen(4000)

console.log('demo start at port 4000')